package edu.pet.Servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.Specialties;
import edu.pet.entity.Vet;

/**
 * Servlet implementation class VetUpdateServlet
 */
public class VetUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VetUpdateServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("type");
		Vet vet = new Vet();
		PetDAO dao = new PetDAO();
		if("toEdit".equals(type)){
			vet = dao.findVetById(Integer.parseInt(request.getParameter("id")));
			List<Specialties> specList = new ArrayList<Specialties>();
			specList = dao.findAllSpecialies();
			request.setAttribute("vet", vet);
			request.setAttribute("specList", specList);
			this.getServletContext().getRequestDispatcher("/user/manage/VetEdit.jsp").forward(request, response);
		}
		if("do".equals(type)){
			vet.setId(Integer.parseInt(request.getParameter("id")));
			vet.setName(request.getParameter("name"));
			vet.setAddress(request.getParameter("address"));
			vet.setTelephone(request.getParameter("telephone"));
			vet.setSpecId(Integer.parseInt(request.getParameter("specId")));
			dao.editVet(vet);
			this.getServletContext().getRequestDispatcher("/servlet/DoctorServlet?type=list").forward(request, response);
		}
		if("toAdd".equals(type)){
			List<Specialties> specList = new ArrayList<Specialties>();
			specList = dao.findAllSpecialies();
			request.setAttribute("specList", specList);
			this.getServletContext().getRequestDispatcher("/user/manage/addVet.jsp").forward(request, response);
		}
		if("doAdd".equals(type)){
			vet.setName(request.getParameter("name"));
			vet.setAddress(request.getParameter("address"));
			vet.setTelephone(request.getParameter("telephone"));
			vet.setSpecId(Integer.parseInt(request.getParameter("spec_id")));
			dao.addVet(vet);
			this.getServletContext().getRequestDispatcher("/servlet/DoctorServlet?type=list").forward(request, response);
		}
		if("del".equals(type)){
			int id = Integer.parseInt(request.getParameter("id"));
			dao.delVet(id);
			this.getServletContext().getRequestDispatcher("/servlet/DoctorServlet?type=list").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
